package com.sleepcamel.resteasyConfigurator.components.tree;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.eclipse.jface.action.Action;

import com.sleepcamel.rest.domain.BaseMethod;
import com.sleepcamel.rest.domain.Mapping;
import com.sleepcamel.rest.domain.Project;
import com.sleepcamel.rest.domain.ProjectPackage;
import com.sleepcamel.rest.domain.method.NonRESTMethod;
import com.sleepcamel.rest.domain.method.RESTMethod;
import com.sleepcamel.resteasyConfigurator.components.tree.strategies.ETreeStrategy;
import com.sleepcamel.resteasyConfigurator.model.command.AddNodeAction;
import com.sleepcamel.resteasyConfigurator.model.command.RemoveNodeAction;

public class TreeActionProvider {

	private static TreeActionProvider instance;
	
	private TreeActionProvider() {
	}

	public static TreeActionProvider getInstance(){
		if ( instance == null ){
			instance = new TreeActionProvider();
		}
		return instance;
	}

	public List<Action> getActionsForType(final Object firstElement, ETreeStrategy treeStrategy) {
		if ( firstElement instanceof Project ) {
			return getActionsForType((Project)firstElement, treeStrategy);
		}
		
		if ( firstElement instanceof ProjectPackage ) {
			return getActionsForType((ProjectPackage)firstElement, treeStrategy);
		}
		
		if ( firstElement.getClass().equals(Mapping.class)) {
			return getActionsForType((Mapping)firstElement, treeStrategy);
		}

		if ( firstElement instanceof BaseMethod ) {
			return getActionsForType((BaseMethod<?>) firstElement, treeStrategy);
		}
		
		return Collections.emptyList();
	}

	private List<Action> getActionsForType(Project project, ETreeStrategy treeStrategy) {
		Action addAction = null;
		switch ( treeStrategy ){
			case PACKAGE:
				addAction = new AddNodeAction(project, ProjectPackage.class, "&Package", "container_object.png");
				break;
			case PUBLISH:
				addAction = new AddNodeAction(project.getChilds().get(0), Mapping.class, "M&apping", "java.png");
				break;
		}
		return Arrays.asList(addAction);
	}
	
	private List<Action> getActionsForType(ProjectPackage projectPackage, ETreeStrategy treeStrategy) {
		Action addPackageAction = new AddNodeAction(projectPackage, ProjectPackage.class, "&Package", "container_object.png");
		
		Action addMappingAction = new AddNodeAction(projectPackage, Mapping.class, "M&apping", "java.png");
		
		Action removeNodeAction = new RemoveNodeAction(projectPackage);
		
		return Arrays.asList(addPackageAction, addMappingAction, removeNodeAction);
	}
	
	private List<Action> getActionsForType(Mapping mapping, ETreeStrategy treeStrategy) {
		Action addRestMethodAction = new AddNodeAction(mapping, RESTMethod.class, "&RestMethod", "hand.png");
		
		Action addNonRestMethodAction = new AddNodeAction(mapping, NonRESTMethod.class, "&Method", "method_public_object.png");
		
		Action removeNodeAction = new RemoveNodeAction(mapping);
		
		return Arrays.asList(new Action[]{addRestMethodAction,addNonRestMethodAction,removeNodeAction});		
	}
	
	private List<Action> getActionsForType(BaseMethod<?> method, ETreeStrategy treeStrategy) {
		Action removeNodeAction = new RemoveNodeAction(method);
		return Arrays.asList(new Action[]{removeNodeAction});
	}
}
